/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.dto.platform.ApplicationElevatorDetailSearchDTO;
import org.springblade.modules.admin.dto.platform.ElevatorCheckDTO;
import org.springblade.modules.admin.entity.EstateApplicationElevator;
import org.springblade.modules.admin.enums.EffectiveStatusEnum;
import org.springblade.modules.admin.service.IEstateApplicationElevatorService;
import org.springblade.modules.admin.vo.EstateApplicationElevatorVO;
import org.springblade.modules.admin.vo.platform.ApplicationElevatorDetailVo;
import org.springblade.modules.admin.vo.platform.ApplicationElevatorStatusDetailVo;
import org.springblade.modules.admin.vo.platform.CheckApplicationElevatorVo;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 物业项目关联申请记录电梯表 控制器
 *
 * @author ysq
 * @since 2022-05-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/estate/application/elevator")
@Api(value = "物业项目关联申请记录电梯表", tags = "物业项目关联申请记录电梯表接口")
public class EstateApplicationElevatorController extends BladeController {

	private final IEstateApplicationElevatorService estateApplicationElevatorService;

	/**
	 * 自定义分页 物业项目关联申请记录电梯表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页", notes = "传入estateApplicationElevator")
	public R<IPage<EstateApplicationElevatorVO>> page(EstateApplicationElevatorVO estateApplicationElevator, Query query) {
		IPage<EstateApplicationElevatorVO> pages = estateApplicationElevatorService.selectEstateApplicationElevatorPage(Condition.getPage(query), estateApplicationElevator);
		return R.data(pages);
	}

	@GetMapping("/getSelectedElevator")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取已经申请关联过的电梯")
	public R<List<Long>> getSelectedElevator() {
		return R.data(estateApplicationElevatorService.getSelectedElevator());
	}


	@PostMapping("/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "获取已经申请关联过的电梯")
	public R<IPage<ApplicationElevatorDetailVo>> list(@RequestBody ApplicationElevatorDetailSearchDTO applicationElevatorDetailSearchDTO) {
		return R.data(estateApplicationElevatorService.list(applicationElevatorDetailSearchDTO));
	}


	@PostMapping("/checkList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "校验电梯申请的列表")
	public R<List<CheckApplicationElevatorVo>> checkList(@RequestBody ApplicationElevatorDetailSearchDTO applicationElevatorDetailSearchDTO) {
		List<EstateApplicationElevator> estateApplicationElevators = estateApplicationElevatorService.listByIds(applicationElevatorDetailSearchDTO.getIds());
		List<CheckApplicationElevatorVo> list = new ArrayList<>(16);

		if (applicationElevatorDetailSearchDTO.getMaintainStartTime() != null) {
			List<Integer> maintainStatus = new ArrayList<>();
			maintainStatus.add(EffectiveStatusEnum.LOSE_EFFICACY.getStatus());
			maintainStatus.add(EffectiveStatusEnum.WAIT_EFFECT.getStatus());
			applicationElevatorDetailSearchDTO.setMaintainStatus(maintainStatus);
		}
		if (applicationElevatorDetailSearchDTO.getTlwStartTime() != null) {
			List<Integer> tlwStatus = new ArrayList<>();
			tlwStatus.add(EffectiveStatusEnum.LOSE_EFFICACY.getStatus());
			tlwStatus.add(EffectiveStatusEnum.WAIT_EFFECT.getStatus());
			applicationElevatorDetailSearchDTO.setTlwStatus(tlwStatus);
		}
		applicationElevatorDetailSearchDTO.setSize(200);
		List<ApplicationElevatorDetailVo> records = estateApplicationElevatorService.listV2(applicationElevatorDetailSearchDTO).getRecords();
		Map<Long, List<ApplicationElevatorDetailVo>> listMap = null;
		if (records.size() > 0) {
			listMap = records.stream().collect(Collectors.groupingBy(ApplicationElevatorDetailVo::getElevatorId));
		}
		for (EstateApplicationElevator estateApplicationElevator : estateApplicationElevators) {
			CheckApplicationElevatorVo checkApplicationElevatorVo = new CheckApplicationElevatorVo();
			BeanUtil.copyProperties(estateApplicationElevator, checkApplicationElevatorVo);
			if (listMap != null && listMap.size() > 0) {
				List<ApplicationElevatorDetailVo> applicationElevatorDetailVos = listMap.get(estateApplicationElevator.getElevatorId());
				if (applicationElevatorDetailVos == null) {
					continue;
				}
				Map<Long, List<ApplicationElevatorDetailVo>> longListMap = applicationElevatorDetailVos.stream().collect(Collectors.groupingBy(ApplicationElevatorDetailVo::getId));
				List<ApplicationElevatorDetailVo> applicationElevatorDetailVos1 = longListMap.get(estateApplicationElevator.getId());
				if (applicationElevatorDetailVos1 != null && applicationElevatorDetailVos1.size() > 0) {
					applicationElevatorDetailVos.removeAll(applicationElevatorDetailVos1);
				}
				if (applicationElevatorDetailVos.size() > 0) {
					list.add(checkApplicationElevatorVo);
					checkApplicationElevatorVo.setList(applicationElevatorDetailVos);
				}
			}

		}
		if (list.size() > 0) {
			return R.data(list);
		} else {
			return R.data(null);
		}

	}


	@ApiOperation(value = "获取审核进度状态数量")
	@GetMapping("getApplicationElevatorStatusDetail")
	public R<ApplicationElevatorStatusDetailVo> getApplicationElevatorStatusDetail(Long formId) {
		return R.data(estateApplicationElevatorService.getApplicationElevatorStatusDetail(formId));
	}

	@ApiOperation(value = "批量审核")
	@PostMapping("update")
	public R<Boolean> update(@RequestBody ElevatorCheckDTO elevatorCheckDTO) {
		return R.data(estateApplicationElevatorService.update(elevatorCheckDTO));
	}
}
